/**
 ****************************************************************************************************
 * @file        adc_battery.c
 * @author      嵌入式小书虫
 * @version     V1.0
 * @date        2020-04-17
 * @brief       key 驱动代码
 ****************************************************************************************************
 *
 * 修改说明
 * V1.0 20200417
 * 第一次发布
 <欢迎关注微信公众号：嵌入式小书虫>
 *
 ****************************************************************************************************
 */

#include "./BSP/adc_battery.h"
//pc1
ADC_HandleTypeDef hadc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */

  /** Common config
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    //Error_Handler();//==========================================
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_VREFINT;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //Error_Handler();//=================================
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* ADC1 clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**ADC1 GPIO Configuration
    PC0     ------> ADC1_IN10
    PC1     ------> ADC1_IN11
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /* USER CODE BEGIN ADC1_MspInit 1 */

  /* USER CODE END ADC1_MspInit 1 */
  }
}

void adc_init(){
    HAL_ADC_MspInit(&hadc1);
	  MX_ADC1_Init();
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */

  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_ADC1_CLK_DISABLE();

    /**ADC1 GPIO Configuration
    PC0     ------> ADC1_IN10
    PC1     ------> ADC1_IN11
    */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1);

  /* USER CODE BEGIN ADC1_MspDeInit 1 */

  /* USER CODE END ADC1_MspDeInit 1 */
  }
} 
 
 uint32_t adc_get_value(uint8_t pchannel)
{
	ADC_ChannelConfTypeDef sConfig = {0};
	uint32_t vadc_value = 0;
	HAL_StatusTypeDef vstatus = HAL_ERROR;
	
	switch (pchannel)
	{
		case 0:
			sConfig.Channel = ADC_CHANNEL_VREFINT;
			sConfig.Rank = ADC_REGULAR_RANK_1;
			sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
		break;
		
		case 1:
			sConfig.Channel = ADC_CHANNEL_11;
			sConfig.Rank = ADC_REGULAR_RANK_1;
			sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
		break;
		
		case 2:
			sConfig.Channel = ADC_CHANNEL_10;
			sConfig.Rank = ADC_REGULAR_RANK_1;
			sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
		break;
		
		default:break;
	}
	
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //Error_Handler();//========================================================
  }
	
	HAL_ADC_Start(&hadc1);
	vstatus = HAL_ADC_PollForConversion(&hadc1, 10);
	if (vstatus == HAL_OK)
	{
		vadc_value = HAL_ADC_GetValue(&hadc1);
	}
	
	return (vadc_value);
}

volatile uint32_t gadc_ref = 0, gadc_bat = 0, gadc_power = 0;
 void sample_volts(void)
{
    double vtemp = 0.0;

    gadc_ref = adc_get_value(0);

    gadc_bat = adc_get_value(2);
    vtemp = 2.4 * gadc_bat;
    vtemp /= gadc_ref;
    gadc_bat = (uint32_t)(vtemp * 1000);

    gadc_power = adc_get_value(1);
    vtemp = 2.4 * gadc_power;
    vtemp /= gadc_ref;
    gadc_power = (uint32_t)(vtemp * 1000);
    
    if (gadc_bat > 4000)
    {
        //gadc_bat_bar_level = 3;
    }
    else if (gadc_bat > 3700)
    {
        //gadc_bat_bar_level = 2;
    }
    else if (gadc_bat > 3500)
    {
        //gadc_bat_bar_level = 1;
    }
    else
    {
        //gadc_bat_bar_level = 0;
    }
} 
 #if 0

ADC_HandleTypeDef hadc;
 
void MX_ADC1_Init(void) {
	GPIO_InitTypeDef ADC_GPIO_init_struct;
  ADC_ChannelConfTypeDef sConfig = {0};
  __HAL_RCC_GPIOC_CLK_ENABLE();
	__HAL_RCC_ADC1_CLK_ENABLE();
	HAL_GPIO_Init(ADC_GPIO_PORT, &ADC_GPIO_init_struct); /* 初始化引脚 */
	
	ADC_GPIO_init_struct.Pin = ADC_GPIO_PIN;             /* 引脚 */
  ADC_GPIO_init_struct.Mode = GPIO_MODE_ANALOG;//GPIO_MODE_OUTPUT_PP;            /* 推挽输出 */
	
	
  hadc.Instance = ADC1;
  hadc.Init.ScanConvMode = DISABLE;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.NbrOfConversion = 1;
  if (HAL_ADC_Init(&hadc) != HAL_OK) {
    //Error_Handler();
  }
 
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
    //Error_Handler();
  }
}
 
uint32_t get_adc_value(void) {
  
    // 读取通道1的ADC值
    uint32_t adcValue = HAL_ADC_GetValue(&hadc);
    return adcValue;
}

#endif





